<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="addCate()">添加分类</el-button>

      <zk-table
        :data="data"
        :columns="columns"
        :expand-type="false"
        :selection-type="false"
        show-index
        index-text="#"
      >
        <template slot="deleted" scope="scope">
          <i
            v-if="scope.row.cat_deleted === false"
            class="el-icon-success"
            style="color: lightgreen"
          />
          <i v-else class="el-icon-error" style="color: #f08889" />
        </template>
        <template slot="slot" scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level === 2" type="warning">三级</el-tag>
        </template>
        <template slot="oper" scope="scope">
          <!-- 修改v-slot="scope" -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="addCate(scope.row)"
            >编辑</el-button
          >
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </zk-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.pagenum"
        :page-sizes="[5, 10, 15, 50]"
        :page-size="form.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="left" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="catename"></el-input>
        </el-form-item>
        <!-- {{ value }} -->
        <el-form-item label="分类父类" >
          <!-- v-if="iscoll" -->
          <el-cascader
            :options="options"
            v-model="value"
            :props="{ label: 'cat_name', value: 'cat_id' }"
            change-on-select="true"
          >
          </el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="cateEn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
      options: [],
      catename: "",
      title:'',
      cat_id:0,
      // iscoll:true,
      dialogVisible: false,
      form: {
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      data: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "deleted",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "slot",
        },
        {
          label: "操作",
          prop: "score",
          type: "template",
          template: "oper",
        },
      ],
    };
  },
  methods: {
    //删除商品分类
    del(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          var res = await this.$http.delete(`categories/${id}`);
          this.hint(res)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //添加 修改 分类确定按钮
    async cateEn() {
      // this.iscoll = true;
      if(this.title == '修改用户'){
          var res = await this.$http.put(`categories/${this.cat_id}`,{
            cat_name:this.catename
          });
          this.hint(res)
      }else{
        var cate = {
          cat_pid: '',
          cat_name: this.catename,
          cat_level: '',
        };
        if(this.value.length > 0){
          cate.cat_pid = this.value[this.value.length-1];
          cate.cat_level = this.value.length;
        }else{
          cate.cat_pid = 0;
          cate.cat_level = 0
        }
        var res = await this.$http.post("categories", cate);
        this.hint(res);
      }
      this.dialogVisible = false;
    },
    //添加 修改 分类 按钮
    async addCate(row) {
      this.catename = '';
      this.value = [];
      this.dialogVisible = true;
      this.title = '添加分类';
      if(row){
        console.log(row);
        // this.iscoll = false;
        this.cat_id = row.cat_id;
        this.title = '修改分类';
        this.catename = row.cat_name;
        if(row.cat_level == 0){
          this.value = [];
        }else if(row.cat_level == 1){
          this.value = [row.cat_pid];
        }else if(row.cat_level == 2){
          var {data:res2} = await this.$http.get(`categories/${row.cat_pid}`)
          console.log(res2);
          this.value = [res2.data.cat_pid,row.cat_pid];
        }
      }
    },
    //提示 成功或失败 的信息
    hint(res) {
      if (res.data.meta.status == 200 || res.data.meta.status == 201) {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "success",
        });
        this.getCate();
        this.$router.push("/categories");
      } else {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "error",
        });
      }
    },
    handleSizeChange(size) {
      this.form.pagesize = size;
      this.getCate();
    },
    handleCurrentChange(num) {
      this.form.pagenum = num;
      this.getCate();
    },
    async getCate() {
      var { data: res } = await this.$http.get("categories", {
        params: this.form,
      });

      this.data = res.data.result;
      this.total = res.data.total;
    },
    async getPage() {
      var { data: res } = await this.$http.get("categories", {
        params: {
          type: 2,
        },
      });
      this.options = res.data;
    },
  },
  created() {
    this.getCate();
    this.getPage();
  },
};
</script>

<style>
.zk-table {
  margin-top: 20px;
}
.el-cascader {
  width: 100%;
}
</style>